<?php

    if(!defined('BASEPATH'))
        exit('No direct script access allowed');

    /*
     * Main page class
     * 
     */

    class Page extends CI_Controller
    {

        var $data;

        public function __construct()
        {

            parent::__construct();

            $this->load->library('rest', array(
                'server' => REST_URL_RENTACAR
            ));

            $this->data['hosting']=HOST_URL_RENTACAR;
            $this->load->model('cms_pro', 'cms');
            $this->load->library('session');
            $this->load->library('template');
            $this->load->helper('clean_html');
            $this->load->helper('language');
            $this->load->helper('urlify');
            $this->load->helper('gallery');
            $this->lang->load ( 'common' );
			$this->lang->load('page');

            require_once("urlify.php");
            $this->load->library('translate', array('lang' => $this->lang->lang()));
            $this->template->set_view("page");

            $this->data['action_footer_1']=$this->rest->get('action1', NULL, 'json');
            $this->data['action_footer_2']=$this->rest->get('action2', NULL, 'json');

        }

        // public function sitemap()
        // {
            // $this->rest->initialize(array(
                // 'server' => REST_URL_EXCURSIONS
            // ));
            // $params=array(
                // 'lang' => $this->lang->lang()
            // );

            // $this->data['action_1']=$this->rest->post('action1', $params, 'json');
            // $this->data['action_2']=$this->rest->post('action2', $params, 'json');

            // $this->rest->initialize(array(
                // 'server' => REST_URL_HOTELS
            // ));

            // $combined=$this->rest->post('popular_destination', array('lang' => $this->lang->lang()));

            // if($combined):

                // $this->data['action_hotels_1']=$combined->we_recommend;
                // $this->data['action_hotels_2']=$combined->special_offer->objects;

            //$this->firephp->log($combined->special_offer->objects);
            // endif;
			
			// $this->data['title']= lang('title-sitemap');
			// $this->data['description']=lang('description-sitemap');
            
            // $this->template->load('page/sitemap', $this->data);

        // }
		
        public function sitemap()
        {
			//load hotels for listing
			$this->rest->initialize(array(
                'server' => REST_URL_HOTELS,
                'http_user' => 'cipa',
                'http_pass' => 'booking',
                'http_auth' => 'basic' // or 'digest' 
            ));
			$params=array(
				"dest_hot" => '',
				"lang" => $this->lang->lang(),
			);
			$this->data['hotels']=$this->rest->post("searchPreview", $params);
			
			$allowedDestinations = array(
				"2" => "Budva",
				"3" => "Herceg Novi",
				"4" => "Kotor",
				"15" => "Petrovac",
				"45" => "Ada Bojana",
				"53" => "Dubrovnik",
				"1" => "Bar",
				"54" => "Ostrog Monastery",
				"55" => "Budva Old Town",
				"48" => "Albania",
			);
			$destinations = $this->rest->post ( 'destinationList', array ('lang' => $this->lang->lang ()) );
			$filtered = array();
			foreach($destinations as $destination) {
				if(isset($allowedDestinations[(string)$destination->ID])) {
					$filtered[] = $destination;
				}
			}			

			$this->data['destinations'] = $filtered;
			
			//load excursions for listing			
            $this->rest->initialize(array(
                'server' => REST_URL_EXCURSIONS
            ));
            $params=array(
                'eb_starting' => 'Any',
                'eb_freetext' => '',
                'exc_sort'    => 'none',
                'lang' => $this->lang->lang()
            );

			$this->data['excursions'] = $this->rest->post('excursions', $params, 'json');

			//load rentacar for listing
            $this->rest->initialize(array(
                'server' => REST_URL_RENTACAR
            ));
			$params = array('lang'=>$this->lang->lang());            

			$this->data['rentacar']=$this->rest->get('all_cars', $params, 'json');

			//load news for listing
			$news=$this->cms->get_news();
            $this->data['news']=$this->translate->trans_res_array($news);

			$this->data['title']= lang('title-sitemap');
			$this->data['description']=lang('description-sitemap');

            $this->template->load('page/sitemap', $this->data);

        }

        public function popup($post)
        {
            $data['page']=$this->translate->trans_row_array($this->cms->get_post($post));
            $html=$this->load->view('page/parts/b_popup', $data, TRUE);
            echo json_encode(array('action' => TRUE, 'html' => $html));

        }

        public function news()
        {
            $news=$this->cms->get_news();
            $this->data['news']=$this->translate->trans_res_array($news);
			$this->data['title']= lang('title-news');
			$this->data['description']= lang('description-news');
            
            $this->template->load('page/news', $this->data);

        }

        public function text_page($id)
        {
            $this->data['page']=$this->translate->trans_row_array($this->cms->get_post($id));
			
			$this->data['title']= str_replace('{|0|}', $this->data['page']['post_title'], lang('title-page')); 
			$this->data['description']= lang('description-page');
            
            $this->template->load('page/read_page', $this->data);

        }

        public function read_news($href)
        {
            $news_ID=idFromUrl($href, '-');
            //$this->firephp->log($news_ID);
            $this->data['page']=$this->translate->trans_row_array($this->cms->get_post($news_ID));
            //$this->firephp->log($this->data['page']);
            $gallery_id=$this->cms->get_gallery_id($news_ID);

            if($gallery_id)
            {
                $this->data['gallery']=$this->cms->get_gallery($gallery_id);
                $this->data['pictures']=$this->cms->get_pictures($this->data['gallery']['ID']);
                //$this->firephp->log($this->data);
            }

			$this->data['title']= str_replace('{|0|}', $this->data['page']['post_title'], lang('title-news')); 
			$this->data['description']= lang('description-news-item');
            
            $this->data['no_gallery_links']=TRUE;
            $this->template->load('page/read_news', $this->data);

        }

        public function map()
        {
            $this->data['locations']=$this->rest->get('locations', NULL, 'json');
			$this->data['title']= lang('title-map');
			$this->data['description']=lang('description-map');
            
            $this->template->load('page/google_map', $this->data);

        }

    }

?>
